/*
  Copyright 2008 Google Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */ 

package pat.solitaire;

import pat.solitaire.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.content.Intent;
import android.net.Uri;

// Base activity class.
public class OldFrenchSolitaire extends Activity {
	private static final int MENU_NEW_GAME  = 1;
	private static final int MENU_RESTART   = 2;
	private static final int MENU_OPTIONS   = 3;
	private static final int MENU_SAVE_QUIT = 4;
	private static final int MENU_QUIT      = 5;
	private static final int MENU_SOLITAIRE = 6;
	private static final int MENU_SPIDER    = 7;
	private static final int MENU_FREECELL  = 8;
	private static final int MENU_FORTYTHIEVES = 9;
	private static final int MENU_STATS     = 10;
	private static final int MENU_HELP      = 11;
	private static final int MENU_DEAL      = 12;
	private static final int MENU_PSSOLITAIRE      = 13;
	private static final int MENU_FAISABLE      = 14;
	private static final int MENU_RULES      = 15;

	// View extracted from main.xml.
	private View mMainView;
	private SolitaireView mSolitaireView;
	private SharedPreferences mSettings;

	private boolean mDoSave;
	public static final String TAG = "solitaire";

	// Shared preferences are where the various user settings are stored.
	public SharedPreferences GetSettings() { return mSettings; }

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mDoSave = true;

		// Force landscape and no title for extra room
		//    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// If the user has never accepted the EULA show it again.
		mSettings = PreferenceManager.getDefaultSharedPreferences(this);
		setContentView(R.layout.main);
		mMainView = findViewById(R.id.main_view);
		mSolitaireView = (SolitaireView) findViewById(R.id.solitaire);
		mSolitaireView.SetTextView((TextView) findViewById(R.id.text));

		//StartSolitaire(savedInstanceState);
	}

	// Entry point for starting the game.
	//public void StartSolitaire(Bundle savedInstanceState) {
	@Override
	public void onStart() {
		super.onStart();
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		if (mSettings.getBoolean("SolitaireSaveValid", false)) {
			SharedPreferences.Editor editor = GetSettings().edit();
			editor.putBoolean("SolitaireSaveValid", false);
			editor.commit();
			// Log.e("load","onStart");
			// If save is corrupt, just start a new game.
			if (mSolitaireView.LoadSave()) {
			    //Log.e("load","loadsave");
				HelpSplashScreen();
				return;
			}
		}
		//Log.e("load","apresonstart");
		mSolitaireView.InitGame(mSettings.getInt("LastType", Rules.PSSOLITAIRE));
		HelpSplashScreen();
	}

	// Force show the help if this is the first time played. Sadly no one reads
	// it anyways.
	private void HelpSplashScreen() {
		if (!mSettings.getBoolean("PlayedBefore", false)) {
			mSolitaireView.DisplayHelp();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_NEW_GAME, 0, R.string.menu_newgame);
		menu.add(0, MENU_RESTART, 0, R.string.menu_restart);
		menu.add(0, MENU_FAISABLE, 0, R.string.menu_faisable);
		menu.add(0, MENU_QUIT, 0, R.string.menu_quit);
		menu.add(0, MENU_HELP, 0, R.string.menu_help);
		menu.add(0, MENU_RULES, 0, R.string.menu_rules);
		menu.add(0, MENU_OPTIONS, 0, R.string.menu_options);
		menu.add(0, MENU_STATS, 0, R.string.menu_stats);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_NEW_GAME:
			mSolitaireView.InitGame(Rules.PSSOLITAIRE);
			break;
		case MENU_RESTART:
			mSolitaireView.RestartGame();
			break;
		case MENU_DEAL:
			mSolitaireView.Deal();
			break;
		case MENU_STATS:
			DisplayStats();
			break;
		case MENU_OPTIONS:
			DisplayOptions();
			break;
		case MENU_FAISABLE:
			DisplaySolution();
			break;
		case MENU_RULES:
			DisplayRules();
			break;
		case MENU_HELP:
			mSolitaireView.DisplayHelp();
			break;
			//case MENU_SAVE_QUIT:
			//mSolitaireView.SaveGame();
			//mDoSave = false;
			//finish();
			//break;
		case MENU_QUIT:
			mDoSave = false;
			finish();
			break;
		}

		return false;
	}

	@Override
	protected void onPause() {
		super.onPause();
		mSolitaireView.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mDoSave) {
			mSolitaireView.SaveGame();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		//    mSettings = getSharedPreferences("com.kmagic.solitaire_preferences", 0);
		mSettings = PreferenceManager.getDefaultSharedPreferences(this);
		mSolitaireView.onResume();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	public void DisplayOptions() {
		mSolitaireView.SetTimePassing(false);
		Intent settingsActivity = new Intent(this, Preferences.class);
		startActivity(settingsActivity);
		//    new Options(this, mSolitaireView.GetDrawMaster());
	}

	public void DisplaySolution() {
		mSolitaireView.SetTimePassing(false);
		new Solution(this, mSolitaireView);
	}

        public void DisplayRules() {
	    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://drive.google.com/file/d/0BxUOAVv2MV12bkFFN2wyUDE5NGM/edit?usp=sharing"));
	    startActivity(i); 
	}

	public void DisplayStats() {
		mSolitaireView.SetTimePassing(false);
		new Stats(this, mSolitaireView);
	}

	public void CancelOptions() {
		setContentView(mMainView);
		mSolitaireView.requestFocus();
		mSolitaireView.SetTimePassing(true);
	}

	public void NewOptions() {
		setContentView(mMainView);
		mSolitaireView.InitGame(mSettings.getInt("LastType", Rules.SOLITAIRE));
	}

	// This is called for option changes that require a refresh, but not a new game
	public void RefreshOptions() {
		setContentView(mMainView);
		mSolitaireView.RefreshOptions();
	}
}
